package org.csu.secondhand.api.controller.fronted;

import org.csu.secondhand.api.entity.Account;
import org.csu.secondhand.api.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @GetMapping("getAccount")
    @ResponseBody
    public Map<Object, Object> getAccount(String id) {
        Map<Object, Object> map = new HashMap<>();
        Account account = accountService.getAccount(id);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", account);
        return map;
    }

    @PostMapping("login")
    @ResponseBody
    public Map<Object, Object> login(String code) {
        Map<Object, Object> map = new HashMap<>();
        Account account = accountService.login(code);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", account);
        return map;
    }

    @PostMapping("initAccount")
    @ResponseBody
    public Map<Object, Object> initAccount(String id,
                                           String username,
                                           int sex,
                                           String headimg) {
        Map<Object, Object> map = new HashMap<>();
        Account account = accountService.getAccount(id);
        if(account.getUsername().equals("") | account.getUsername()==null) {
            account.setUsername(username);
        }
        if(account.getSex()==0) {
            account.setSex(sex);
        }
        if(account.getHeadimg().equals("") | account.getHeadimg()==null) {
            account.setHeadimg(headimg);
        }
        if(account.getBirthday().equals("") | account.getBirthday()==null) {
            account.setBirthday("2001-01-01");
        }
        accountService.editAccount(account);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", account);
        return map;
    }

    @PostMapping("editAccount")
    @ResponseBody
    public Map<Object, Object> editAccount(String id,
                                           String username,
                                           int sex,
                                           String birthday,
                                           String department,
                                           String address) {
        Map<Object, Object> map = new HashMap<>();
        Account account = accountService.getAccount(id);
        account.setUsername(username);
        account.setSex(sex);
        account.setBirthday(birthday);
        account.setDepartment(department);
        account.setAddress(address);
        accountService.editAccount(account);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", account);
        return map;
    }

    @PostMapping("changeAvatar")
    @ResponseBody
    public Map<Object, Object> changeAvatar(String id, String url) {
        Map<Object, Object> map = new HashMap<>();
        Account account = accountService.getAccount(id);
        account.setHeadimg(url);
        accountService.editAccount(account);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", account);
        return map;
    }
}
